//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet.Data
{
    public class ConnectionParameter: IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("parameter"))
            {
                Name = c.GetOptionalString("name");
                SqlType = c.GetOptionalInteger("sqlType", 0);
                ParameterType = c.GetOptionalEnum<ParameterType>("parameterType", ParameterType.Prompt);
                RefreshOnChange = c.GetOptionalBoolean("refreshOnChange", false);
                Prompt = c.GetOptionalString("prompt");
                bool? b = c.GetOptionalBoolean("boolean");
                if (b != null)
                    Value = b.Value;
                double? d = c.GetOptionalDouble("double");
                if (d != null)
                    Value = d.Value;
                int? i = c.GetOptionalInteger("integer");
                if (i != null)
                    Value = i.Value;
                string s = c.GetOptionalString("string");
                if (s != null)
                    Value = s;
                s = c.GetOptionalString("cell");
                if (s != null)
                {
                                                  Worksheet ws = c.GetData<Worksheet>();
                                                  Workbook wb = c.GetData<Workbook>();
                    SimpleAction action = delegate
                                              {
                                                  RangeReference v = null;
                                                  if (ws != null)
                                                      v = ws.GetRangeReference(s);
                                                      //wb.NamingConvention.TryParseWorksheetRangeReference(s, null, ws, out v);
                                                  else
                                                  {
                                                      v = wb.GetRangeReference(s);
                                                      //wb.NamingConvention.TryParseRangeReference(s,wb, out v);
                                                  }
                                                  Value = v.Range[0,0];
                                              };
                    c.GetData<WorkbookResolver>().ResolveActions.Add(action);
                    //Value = ws[s];
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("parameter"))
            {
                c.SetOptionalString("name", Name);
                c.SetOptionalInteger("sqlType", SqlType, 0);
                c.SetOptionalEnum<ParameterType>("parameterType", ParameterType, ParameterType.Prompt);
                c.SetOptionalBoolean("refreshOnChange", RefreshOnChange, false);
                c.SetOptionalString("prompt", Prompt);
                if (Value is bool)
                    c.SetBoolean("boolean", (bool)Value);
                if (Value is double)
                    c.SetDouble("double", (double)Value);
                if (Value is int)
                    c.SetInteger("integer", (int)Value);
                if (Value is string)
                    c.SetString("string", (string)Value);
                if (Value is Cell)
                    c.SetString("cell", ((Cell)Value).Label); //\\ full label?
            }
        }

        #endregion

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _sqlType;

        public int SqlType
        {
            get { return _sqlType; }
            set { _sqlType = value; }
        }
        private ParameterType _parameterType;

        public ParameterType ParameterType
        {
            get { return _parameterType; }
            set { _parameterType = value; }
        }
        private bool _refreshOnChange;

        public bool RefreshOnChange
        {
            get { return _refreshOnChange; }
            set { _refreshOnChange = value; }
        }
        private string _prompt;

        public string Prompt
        {
            get { return _prompt; }
            set { _prompt = value; }
        }
        private object _value;

        public object Value
        {
            get { return _value; }
            set { _value = value; }
        }
	
	
	
	
	
	
	
	
    }
}
